// 全局apiurl
// var urlpath = location.href.indexOf("?")+1;
// urlpath = location.href.substring(urlpath);
// var arr=urlpath.split("&");
// uesrMessage ={
// }
// arr.forEach(
//     function(item){
//         var brr = [];
//         brr=item.split("=");
//         debugger;
//         x[brr[0]]= brr[1];
//     }
// )
// var  userId = uesrMessage.userId;
// var  bzId = uesrMessage.bzId;

// var baseUrl  = "http://10.36.1.67/bzapp/";
// var userId = ;
// var bzId = bzId;
//值班表
var DateTime = new Date();
var year =  DateTime.getFullYear();
var month = DateTime.getMonth()+1;
var from = year+"-"+month+'-1';


var to = "";
if(month==12){
   to = (year+1)+'-1-1'
}else{
    to =year+"-"+(month+1)+'-1';
}

var peopleList = [];
//获取班组简介
var fd = new FormData();
fd.append("json",JSON.stringify({
    "userId":userId,
    "data":{
        "bzId":bzId
    }
}));
// 人员简介字段渲染

$.ajax(
    {
        url:baseUrl+'api/Culture/GetMeetAbstract',
        data:{
            json:JSON.stringify({
                "userId":userId,
                "data":{
                    "bzId":bzId
                }
            })
        },
        type:"POST",
        // contentType:false,
        // processData:false,
        success:function(data){
            console.log(data);
            if(data.code==0){
                classinstru(data.data)
                $(".topLeft").html(data.data.BzName);
            }
            
        }
    }
)
function classinstru(item){
    var x = '<dl style="margin-left: 30px;">'+
									'<dt>班组成员'+item.Member+'人</dt>'+
									'<dt>工程师'+item.Engineer+'人</dt>'+
									'<dt>技师'+item.Technician+'人</dt>'+
								'</dl>'+
								'<dl>'+
									'<dt>大专及以上学历'+item.Education+'人</dt>'+
									'<dt>助理工程师'+item.AssistantEngineer+'人</dt>'+
									'<dt>高级工'+item.Expert+'人</dt>'+
								'</dl>'+
								'<dl>'+
									'<dt>中共党员'+item.PartyMember+'人</dt>'+
									'<dt>高级技师'+item.ExpertTechnician+'人</dt>'+
									'<dt>平均年龄'+(Number(item.AverageAage)).toFixed(0)+'岁</dt>'+
								'</dl>'	
   $("#insterNEST").html(x);
}
//获取文化墙信息
$.ajax(
    {
        // contentType:"application/json; charset=utf-8",
        url:baseUrl+'api/Culture/GetWallInfo',
        data:fd,
        type:"POST",
        contentType:false,
        processData:false,
        success:function(data){
            if(data.code==0){
                mainclass(data.data);
            }
        }
    }
)
function mainclass(item){
    $("#textareaW").html(item.summary);
    // $("#classhope").val('是滴是滴')
    $("#classhope").val(item.vision);
     if(item.slogan){
                var xstring = item.slogan;
                if(xstring.length>20){
                   xstring =  xstring.substring(0,20)+"...";
                }
                $(".titlep").html(xstring);
        }
    // console.log(document.getElementById("#classhop"))
    // document.getElementById("#classhop").innerHTML="11111"
    var arr =[];//班组荣誉
    var brr = [];//风采剪影
    item.pics.forEach(
        function(obj){
            if(obj.filetype==1){
                arr.push(obj);
            }else{
                brr.push(obj);
            }
        }
    )
    console.log(arr,brr);
    var arrObj = "";
    var brrObj = "";
    for(var j=0;j<arr.length;j++){
        arrObj +='<img src="'+arr[j].filepath+'" alt="">'
    }
    $(".honour").html(arrObj);
    for(var h=0;h<brr.length;h++){
        brrObj += '<img src="'+brr[h].filepath+'" alt="">'
    }
    $(".face").html(brrObj);
}
//保存、编辑文化墙信息
var SaveWallInfo = new FormData();
var saveParams ={
       userId:userId,
       data:{
        bzId:bzId,
        summary:"",
        concept:"",
        slogan:"",
        vision:""
       } 
}
SaveWallInfo.append(
    'json',JSON.stringify(saveParams)
)
$.ajax(
    {
        url:baseUrl+'api/Culture/SaveWallInfo',
        data:SaveWallInfo,
        type:"POST",
        contentType:false,
        processData:false,
        success:function(data){
            console.log(data);
        }
    }
)
//上传风采剪影、班组荣誉
var UpdateFile = new FormData();
var UpdateFileInfo ={
       userId:userId,
       data:{
        bzId:bzId,
        deletefileids:"",
        description:"",
        filetype:"0",
       } 
}
UpdateFile.append(
    'json',JSON.stringify(UpdateFileInfo)
)
$.ajax(
    {
        url:baseUrl+'api/Culture/UploadFile',
        data:UpdateFile,
        type:"POST",
        contentType:false,
        processData:false,
        success:function(data){
            console.log(data);
        }
    }
)
//获取班组成员
var classpeople = new FormData();
$.ajax(
    {
        url:baseUrl+'api/Culture/GetDeptMembers',
        type:"POST",
        data:{
            json:JSON.stringify({
                "userId":userId,
                "data":{
                    "bzId":bzId
                }
            })
        },
        success:function(data){
            if(data.code==0){
                peopleList = data.data;
                peoplelHonner = data.data.slice(1)

                var y = '<img src="'+(peopleList[0].Photo?peopleList[0].Photo:'./img/03/person.png')+'" alt="" />'+
								'<dl>'+
									'<dd>'+peopleList[0].Name+'</dd>'+
									'<dt>'+peopleList[0].Quarters+'</dt>'+
								'</dl>'
                $(".leader").html(y)
                getperson();
            }
            console.log(peopleList);
            // peopleList = 
        }
    }
)
function getperson(){
    var x = "";
    if(peoplelHonner.length>14){
        peoplelHonner = peoplelHonner.slice(0,14)
    }
    for(var i=0;i<peoplelHonner.length;i++){
           x +=  '<div>'+
                    '<img src="'+(peoplelHonner[i].Photo?peoplelHonner[i].Photo:'./img/03/person.png')+'" alt="" />'+
                    '<dl>'+
                        '<dd>'+peoplelHonner[i].Name+'</dd>'+
                        '<dt>'+peoplelHonner[i].Quarters+'</dt>'+
                    '</dl>'+
                '</div>'
    }
    $(".member").html(x);
}
//月度工作任务评分
$.ajax(
    {
        url:baseUrl+'api/Culture/GetMonthScore',
        data:{json:JSON.stringify(
            {
                "userId":userId,
                "data":{
                    "year":year,
                    "month":month
                }
            }
        )},
        type:"POST",
        // contentType:false,
        // processData:false,
        success:function(data){
            // charts(data.data)
        }
    }
)
function charts(item) {
    var chartName = [];
    var chartValue = [];

    item.forEach(
        function(list){
            chartName.push(list.UserName);
            chartValue.push(list.Score);
        }
    )
    var obj = document.getElementById('charts')
    var data5 = [
        [10, 52, 25, 36, 30, 30, 29,9,9,9,9,9,9,9,9],
        [2, 3, 20, 34, 90, 0, 20,31,13,31,13,13,13,13],
        [0, 5, 20, 34, 30, 30, 22,45,45,45,45,45,45,5,45]
    ]
    var option = {
        tooltip: {
            trigger: 'axis'
        },
        xAxis: [{
            type: 'category',
            data: chartName,
            axisLine: {
                lineStyle: {
                    color: '#cccccc'
                }
            }
        }],
        yAxis: [{
            type: 'value',
            min: 0,
            axisLabel: {
                formatter: '{value}',
                textStyle: {
                    color: '#cccccc'
                }
            },
            axisLine: {
                lineStyle: {
                    color: '#cccccc'
                }
            }
        },
            {
                type: 'value',
                min: 0,
                axisLabel: {
                    formatter: '{value}',
                    textStyle: {
                        color: '#223550'
                    }
                },
                axisLine: {
                    lineStyle: {
                        color: '#223550'
                    }
                }

            }
        ],
        // dataZoom: [
        //     {
        //         show: true,
        //         realtime: true,
        //         start: 0,
        //         end: 50
        //     },
        //     {
        //         type: 'inside',
        //         realtime: true,
        //         start: 0,
        //         end: 50
        //     }
        // ],
        series: [
            {
                type: 'bar',
                barWidth: '20',
                data: chartValue,
                itemStyle: {
                    normal: {
                        label: {
                            show: true,
                            position:'top',
                            color:'#bfc2d3'
                        },
                        color: '#009ffa',
                    },
                }
            }
        ]
    };
    var Chart4 = echarts.init(obj)
    Chart4.setOption(option);
}
//获取班组月考勤数据
$.ajax(
    {
        url:baseUrl+'api/Attendance/GetDeptAttendance',
        data:{json:JSON.stringify(
            {
                "userId":userId,
                "data":{
                    "year":year,
                    "month":month
                }
            }
        )},
        type:"POST",
        // contentType:false,
        // processData:false,
        success:function(data){
            studyDate(data.data)
        }
    }
)

function studyDate(list){
    var arrlist = ""
    for(var i=0;i<list.length;i++){
        arrlist +='<tr><td class="firsttd">'+
                // '<div class="firstIO" id="firstIo">'+
                //   '<img src="'+(list[i].Photo?list[i].Photo:"./img/03/person.png")+'" alt="">'+
                // '</div>'+
                '<h4>'+list[i].UserName+'</h4>'+
                // '<p>'+(list[i].RoleName?list[i].RoleName:"")+'</p>'+
            '</td>'
            arrlist += '<td>'+
                        '<h4>'+list[i]['Data'][list[i]['Data'].length-1].Times+'</h4>'+
                        '<p>'+list[i]['Data'][list[i]['Data'].length-1].Category+'(次)</p>'+
                    '</td>';
            // if(list[i]['Data'].length!=9){
            //     arrlist += '<td>'+
            //         '<h4>0</h4>'+
            //         '<p>出勤(次)</p>'+
            //         '</td>';
            // }
            for(var j=0;j<list[i]['Data'].length-1;j++){
                if(list[i]['Data'][j].Category=="值班"||list[i]['Data'][j].Category=="出勤"){
                    arrlist += '<td>'+
                        '<h4>'+list[i]['Data'][j].Times+'</h4>'+
                        '<p>'+list[i]['Data'][j].Category+'(次)</p>'+
                    '</td>';
                }else{
                    arrlist += '<td>'+
                        '<h4>'+list[i]['Data'][j].Hours+'</h4>'+
                        '<p>'+list[i]['Data'][j].Category+'(h)</p>'+
                    '</td>';
                }
            }
            arrlist +='</tr>'
    }
    $("#morework").html(arrlist);
}

$.ajax(
    {
        url:baseUrl+'api/Attendance/GetDutyData',
        data:{json:JSON.stringify(
            {
                "userId":userId,
                "data":{
                    "from":year+"-"+month+'-1',
                    "to":to
                }
            }
        )},
        type:"POST",
        // contentType:false,
        // processData:false,
        success:function(data){
            var datapeople = data.data.pop();
            var xinqi = DateTime.getDay();
            var dateArr = [];
            data.data.forEach(
               function(item,index){
                 dateArr.push(
                     {
                        Data: item.Data,
                        index: index+1
                     }
                 )
               }
            )
            work(xinqi,dateArr)
        }
    }
)
//创建值班表
function work(index,list){
    var worklist = "";
    worklist += '<tr><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr><tr>';
    var a = [];
    var c = [];
    var d = [];
    var DateTime = new Date();
    var days =  DateTime.getDate();
    var setflag=0;
    console.log(days,index)
    if(index==0){
        index = 7;
    }
    if(index>(days%7)){
        day = (index-(days%7))+1;

    }else{
        day = 7+(index-(days%7))+1;
        setflag = days+day;
    }
    for(var k=0;k<day;k++){
        a.push(
            {
                Data: ""
            }
        )
    }
    c = a.concat(list);
    
    for(var w=0;w<(42-c.length)%7;w++){
        d.push(
            {
                Data: ""
            }
        )
    }
    c = c.concat(d);
    for(var q=0;q<c.length/7;q++){
        worklist += '<tr>'
        for(var o=0;o<7;o++){
            if((q*7+o)==(days+day-1)){
                worklist +='<td class="dayactive">'+
                '<span>'+((c[7*q+o].index)?(c[7*q+o].index):"")+'</span>'+
                '<p>'+((c[7*q+o].Data)?(c[7*q+o].Data):"")+'</p>'+
                '</td>'
            }else{
                worklist +='<td>'+
                '<span>'+((c[7*q+o].index)?(c[7*q+o].index):"")+'</span>'+
                '<p>'+((c[7*q+o].Data)?(c[7*q+o].Data):"")+'</p>'+
                '</td>'
            }
        }
        worklist += '</tr>'
    }
    $("#newtable").html(worklist);
}
//获取学习园地
$.ajax(
    {
        url:baseUrl+'api/Culture/GetLearningGardens',
        data:fd,
        type:"POST",
        contentType:false,
        processData:false,
        success:function(data){
            console.log(data);
            studyarea(data.data);
        }
    }
)
function studyarea(itemlist){
    var studyarealist = "";
    studyarealist +='<h3><i>学</i><i>习</i><i>园</i><i>地</i></h3>';
    for(var i=0;i<itemlist.length;i++){
        // studyarealist +='<a target="_blank" download href="'+itemlist[i].filepath+'">'+itemlist[i].filename+'</a>';
        studyarealist +='<div class="bg-tab" data-url="'+itemlist[i].filepath+'"><a'+
        // download   target="_blank" href="'+itemlist[i].filepath+'"
        '>'+itemlist[i].filename+'</a></div>';
    }
    $("#h-commoms").html(studyarealist);
}
//月度工作任务

var DateTimeNew = new Date();
var yearnew = DateTimeNew.getFullYear();
var monthnew = DateTimeNew.getMonth()+1;
if(monthnew==1){
    yearnew--;
    monthnew =12;
}else{
}

if(monthnew<10){
    monthnew="0"+monthnew;
}
var fromnew = yearnew+"-"+monthnew+"-01"
var fq = new FormData();
fq.append("json",JSON.stringify({
    "userId":userId,
    "data":{
        // "bzId":bzId,
        "time":fromnew,
        "departmentid":bzId
    }
}));
$.ajax(
    {
        url:baseUrl+'api/Performance/PerformanceSelect',
        data:{json:JSON.stringify(
            {
                "userId":userId,
                "data":{
                    "time":fromnew,
                     "departmentid":bzId
                }
            }
        )},
        type:"POST",
        // contentType:false,
        // processData:false,
        success:function(data){
            console.log(data);
             if(data.code==0){
                 mainTitle(data.data)
             }
             
            // studyarea(data.data);
        }
    }
)
function mainTitle(data){
    console.log(data);
    var sorceList = [];
    var titleThing;
    var monthD = DateTimeNew.getMonth();
    if(monthD == 0){
        monthD = 12
    }
    if (!data) {
        var newDateMsg = "<span>" + monthD + "月份绩效考核</span>" +
            "<span>班组绩效总额0元</span>";
        $("#warning").css("display", "block");
        $("#newDate").html(newDateMsg);
        $(".data-table").css("display", "none");
        return false;
    }
    var upstatus = data.up;
    if (!upstatus) {
        var newDateMsg = "<span>" + monthD + "月份绩效考核</span>" +
            "<span>班组绩效总额0元</span>";
        $("#warning").css("display", "block");
        $("#newDate").html(newDateMsg);
        $(".data-table").css("display", "none");
        return false;
    }
    sorceList = data.score;
    titleThing = data.title;
    var nameList = (titleThing.name).split(",");

    var strTable = "<tbody></tr>";
    strTable +="<th></th>"

    var tabletop;
    for(var i=0;i<nameList.length;i++){
            tabletop += '<th>'+nameList[i]+"</th>";
    }
    $("#table-top").html(tabletop)
    strTable += "</tr>";
    var scoreValue = 0;

    var tablecontent;

    var tableend;
    for(var j=0;j<sorceList.length;j++){
        tablecontent +=Number((sorceList[j]['score'].split(","))[0]);
       
        tablecontent +="<tr>"
        tableend +='<tr><td>'+sorceList[j]['username']+'</td></tr>'
        var scorechange = sorceList[j]['score'].split(",");
        var sortchange = sorceList[j]['sort'].split(",");
            for(var k=0;k<nameList.length;k++){
                tablecontent += '<td>'+scorechange[Number(sortchange[k])]+"</td>";
               
            }
            scoreValue += Number(scorechange[0]);
        tablecontent +="</tr>"
    }
    $("#tob-content").html(tablecontent);
    $("#table-end").html(tableend);
    var newDateMsg = "<span>"+ monthD +"月份绩效考核</span>"+
    "<span>班组绩效总额"+
    scoreValue
    +"元</span>";
    newtable()

 

   
    strTable += "</tbody>";
    if(sorceList.length==0){
        // $("#warning").css("display","block");
    }else{
         $("#new-meassage").html(strTable);
        $("#newDate").html(newDateMsg)
    }

}

var month = (new Date()).getMonth()+1;
setTimeout(
    function(){
        $("#kaoqin").html('<span>'+month+'月考勤表</span>');
        $("#zhiban").html('<span>'+month+'月值班表</span>');
    },2000
)
//班务接口
$.ajax({
    url: baseUrl1+'api/MenuConfig/ModuleShow',
    data: {json:JSON.stringify({
        id:regDeptId,
        themetype: 0
    })},
    type: "POST",
    // contentType:false,
    // processData:false,
    success: function (data) {
        var newdata = data.Data;
        if(newdata.kqb==0){
        $("#select-new").remove();
           $("#select-4").remove();
        }
        if(newdata.zbb==0){
            $("#select-2").remove();
           $("#select-5").remove();
        }
        if(newdata.jxkhb==0){
            $("#select-3").remove();
           $("#charts").remove();
        }
        $('.ul_nav li').eq(0).addClass('active');
        $('.ul_content li').eq(0).css("display","block");
    }
})